using System;
using System.Collections.Generic;
using OracleInternal.Common;

namespace OracleInternal.TTC.Accessors
{
	internal class TTCPLSQLAssociativeArrayAccessor : Accessor
	{
		internal int m_noOfElements;

		internal List<ArraySegment<byte>>[] m_plsqlAssociativeArray;

		internal int[] m_sizeOfEachElement;

		internal TTCPLSQLAssociativeArrayAccessor(ColumnDescribeInfo colMetaData, MarshallingEngine marshallingEngine)
			: base(colMetaData, marshallingEngine, bForBind: true)
		{
		}

		internal override bool UnmarshalOneRow()
		{
			if (m_bNullByDescribe)
			{
				return false;
			}
			m_noOfElements = (int)m_marshallingEngine.UnmarshalUB4();
			m_plsqlAssociativeArray = new List<ArraySegment<byte>>[m_noOfElements];
			m_sizeOfEachElement = new int[m_noOfElements];
			int num = 0;
			List<ArraySegment<byte>> list = null;
			if (m_colMetaData.m_dataType == 1 || m_colMetaData.m_dataType == 9 || m_colMetaData.m_dataType == 96)
			{
				for (int i = 0; i < m_noOfElements; i++)
				{
					num = 0;
					list = null;
					m_marshallingEngine.m_oraBufRdr.StartAccumulatingColumnData(null, 0);
					m_marshallingEngine.UnmarshalCLR_ScanOnly(m_colMetaData.m_maxLength, out list, ref num);
					m_marshallingEngine.m_oraBufRdr.StopAccumulatingColumnData();
					if (-1 == m_marshallingEngine.ProcessIndicator(num <= 0, num))
					{
						num = 0;
					}
					m_plsqlAssociativeArray[i] = list;
					m_sizeOfEachElement[i] = num;
				}
			}
			else if (m_colMetaData.m_dataType == 2 || m_colMetaData.m_dataType == 3 || m_colMetaData.m_dataType == 4 || m_colMetaData.m_dataType == 6)
			{
				for (int j = 0; j < m_noOfElements; j++)
				{
					num = 0;
					list = null;
					m_marshallingEngine.m_oraBufRdr.StartAccumulatingColumnData(null, 0);
					m_marshallingEngine.UnmarshalCLR_ScanOnly(m_colMetaData.m_maxLength, out list, ref num);
					m_marshallingEngine.m_oraBufRdr.StopAccumulatingColumnData();
					if (-1 == m_marshallingEngine.ProcessIndicator(num <= 0, num))
					{
						num = 0;
					}
					m_plsqlAssociativeArray[j] = list;
					m_sizeOfEachElement[j] = (byte)num;
				}
			}
			else if (m_colMetaData.m_dataType == 12)
			{
				for (int k = 0; k < m_noOfElements; k++)
				{
					num = 0;
					list = null;
					m_marshallingEngine.m_oraBufRdr.StartAccumulatingColumnData(null, 0);
					m_marshallingEngine.UnmarshalCLR_ScanOnly(7, out list, ref num);
					m_marshallingEngine.m_oraBufRdr.StopAccumulatingColumnData();
					if (-1 == m_marshallingEngine.ProcessIndicator(num <= 0, num))
					{
						num = 0;
					}
					m_plsqlAssociativeArray[k] = list;
					m_sizeOfEachElement[k] = (byte)num;
				}
			}
			else if (m_colMetaData.m_dataType == 100 || m_colMetaData.m_dataType == 101)
			{
				for (int l = 0; l < m_noOfElements; l++)
				{
					num = 0;
					list = null;
					m_marshallingEngine.m_oraBufRdr.StartAccumulatingColumnData(null, 0);
					m_marshallingEngine.UnmarshalCLR_ScanOnly(TTCBinaryFloatAccessor.BINARY_FLOAT_MAX_LENGTH, out list, ref num);
					m_marshallingEngine.m_oraBufRdr.StopAccumulatingColumnData();
					if (-1 == m_marshallingEngine.ProcessIndicator(num <= 0, num))
					{
						num = 0;
					}
					m_plsqlAssociativeArray[l] = list;
					m_sizeOfEachElement[l] = (byte)num;
				}
			}
			else if (m_colMetaData.m_dataType == 23)
			{
				for (int m = 0; m < m_noOfElements; m++)
				{
					num = 0;
					list = null;
					m_marshallingEngine.m_oraBufRdr.StartAccumulatingColumnData(null, 0);
					m_marshallingEngine.UnmarshalCLR_ScanOnly(m_colMetaData.m_maxLength, out list, ref num);
					m_marshallingEngine.m_oraBufRdr.StopAccumulatingColumnData();
					if (-1 == m_marshallingEngine.ProcessIndicator(num <= 0, num))
					{
						num = 0;
					}
					m_plsqlAssociativeArray[m] = list;
					m_sizeOfEachElement[m] = num;
				}
			}
			return false;
		}

		internal List<ArraySegment<byte>>[] GetPlSqlAssociativeArray()
		{
			return m_plsqlAssociativeArray;
		}

		internal int[] GetElementSizes()
		{
			return m_sizeOfEachElement;
		}
	}
}
